//****************************************************************
//* Copyright (c) 2008 Ford Motor Company. All Rights Reserved.
//*
//* $$Workfile:   EconomicIndexValuesUpdatedFindQuery.java  $$
//* $$Revision:   1.2  $$
//* $$Author:   JLOPE262  $$
//* $$Date:   Nov 24 2008 07:16:38  $$
//*
//*****************************************************************
package ar.com.ford.it.fullEconomics.persistence.dao.query;

import java.sql.Timestamp;
import oracle.toplink.expressions.Expression;
import oracle.toplink.expressions.ExpressionBuilder;
import oracle.toplink.queryframework.ReportQuery;
import ar.com.ford.it.fullEconomics.persistence.dto.Teese01Economic;
import ar.com.ford.it.fullEconomics.persistence.dto.Teese03Econvalue;
import com.ford.it.persistence.impl.toplink.CustomExpressionFactory;

public class EconomicIndexValuesUpdatedFindQuery implements CustomExpressionFactory {
    
    public Expression createExpression(ExpressionBuilder builder, Object[] parameters) {        
        Teese01Economic economic = (Teese01Economic) parameters[0];
        Timestamp minDate = (Timestamp) parameters[1];
        Timestamp maxDate = (Timestamp) parameters[2];
        String actualOrForecast = (String) parameters[3];
        Expression exp = null;
        ReportQuery reportQuery = new ReportQuery(builder);
        
        reportQuery.setReferenceClass(Teese03Econvalue.class);
        reportQuery.addMinimum("eese03EcvalueEffectivDateY", builder.get("eese03EcvalueEffectivDateY"));        

        exp = builder.get("eese03PlantCodeC").equal(economic.getEese01PlantCodeC());
        exp = exp.and(builder.get("eese03EcvalueTypeCodeC").equalsIgnoreCase(actualOrForecast));
        exp = exp.and(builder.get("teese01Economic").equal(economic));
        exp = exp.and(builder.get("eese03EcvalueEffectivDateY").greaterThanEqual(minDate));
        exp = exp.and(builder.get("eese03EcvalueEffectivDateY").lessThanEqual(maxDate));
        // FIXME Decidir si borrar o quitar el siguiente comentario
        // exp = exp.and(builder.get("eese03EcvalueEffectivDateY").equal(builder.subQuery(reportQuery)));        

        return exp;
    }

}
